<!--
 * @Author: your name
 * @Date: 2021-04-10 16:10:36
 * @LastEditTime: 2021-06-02 13:10:09
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ws-shop\src\views\register.vue
-->
<template>
  <div class="register">
    <div class="register-main">
      <h2>注册账号</h2>
      <van-form>
        <van-field
          autocomplete="off"
          v-model="nickname"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
          clickable
          :value="nickname"
        />
        <van-field
          autocomplete="off"
          v-model="username"
          name="账号"
          label="账号"
          placeholder="账号"
          :rules="[{ required: true, message: '请填写账号' }, {pattern:/^\w{8,}$/}]"
          clickable
          :value="username"
        />
        <van-field
          autocomplete="off"
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }, {pattern:/^\w{6,}$/}]"
          clickable
          :value="password"
        />
        <van-field
          autocomplete="off"
          v-model="passwordx"
          type="password"
          name="确认密码"
          label="确认密码"
          placeholder="确认密码"
          :rules="[{ required: true, message: '请确认密码' }, {pattern:/^\w{6,}$/}]"
          clickable
          :value="passwordx"
        />
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit" @click="register">
            提交
          </van-button>
        </div>
      </van-form>
      <div class="toLogin">
        <p>
          <router-link to="/login">已有账号？请登录</router-link>
        </p>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { Form, Toast } from 'vant'

Vue.use(Form, Toast)

export default {
  data () {
    return {
      username: '',
      password: '',
      nickname: '',
      passwordx: ''
    }
  },
  methods: {
    register () {
      this.$http.getUsers({
        username: this.username,
        password: this.password,
        nickname: this.nickname,
        passwordx: this.passwordx
      }).then(res => {
        if (res) {
          Toast.success('注册成功')
          setTimeout(() => {
            this.$router.push({
              path: '/login'
            })
          }, 2000)
        } else {
          Toast.fail('注册失败')
        }
      })
      if (this.username === '' || this.password === '') {
        return
      }
      if (
        !this.username.match(
          /^(\w{1,})+@([a-zA-Z]{2,3})+((\.[a-zA-Z]{2,3}))$/
        ) &&
        !this.password.match(/^\w{6,}$/)
      ) {
        return false
      }
    }
  }
}
</script>

<style lang="scss">
* {
  margin: 0;
  padding: 0;
}
html,
body,
.register {
  height: 100%;
}
.register {
  display: flex;
  flex-direction: column;
}
.register-main {
  flex: 1;
  padding: 100px 0;
  text-align: center;
  h2{
    line-height: 2;
    color: #1989fa;
  }
}
.toLogin {
  p {
    line-height: 2;
    color: blue;
    font-size: 12px;
  }
}
</style>
